<?php

/**
 * BNGOrderProcess.php
 * description:
 * Created by www.
 * User: SCF
 * Date: 2018/9/19 14:06
 *
 */
/*
 *  boongo没有贡献，奖金，所有返奖奖金都是注单里的win字段
 *
 */

namespace App\Console\Commands\ThirdOrder;

use App\Services\ThirdAPI\BNGAPIService;


use App\Services\ThirdAPI\GameApiService;
use App\Services\UserService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;

class BNGOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'bng';
    protected $description = 'BNG电子注单拉取';

    /**
     * @var BNGAPIService
     */
    protected $bngService;
    protected $reportOrderType;
    protected $gameNames;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::BNG_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::BNG_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;
    //const TIME_DIFFERENCE = 28800; //美东时间比utc(统一时间)多4h
    /**
     * 初始化信息，子类需要重写这个方法
     * @param null $argv
     * @throws \Exception
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "BNG";
        $this->bngService = new BNGAPIService();
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_EGAME;
        $this->reportOrderType = self::GAME_TYPE_EGAME;
        $this->gameNames = [];
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     * @return mixed
     * @throws \Exception
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = date('Y-m-d H:i:s',strtotime($startTime));
        // 拉取 $this->timeInterval 的数据，减3秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = date('Y-m-d H:i:s',min(time(), strtotime($startTime) + $this->timeInterval - 3));
        return $this->bngService->getOrders($startTime, $endTime);
    }

    /**
     *  处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     * @param $data
     */
    public function load($data)
    {
        $this->updateTime = $data['update_time'];
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money']??0;
        $this->bonusMoney = $data['bonus_money']??0;
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];
    }

    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey)
    {
        Redis::connection()->set($this->updateRedisKey, $this->latestUpdateKey);
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }
}